﻿/* Serialize Plug-in Context
 * Copyright Andrew Swerlick - Engage, Inc. 2010
 * is free software: you can redistribute it and/or modify it under the terms of the 
 * GNU General Public License as published by the Free Software Foundation, 
 * either version 3 of the License, or (at your option) any later version.
 * Serialize Plug-in Context is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with Serialize Plug-in Context. 
 * If not, see http://www.gnu.org/licenses/.
 ***********************************************/

using System;
using Microsoft.Xrm.Sdk;
using System.Runtime.Serialization;

namespace SerializePluginContext
{
    /// <summary>
    /// An implementation of IPluginExecutionContext designed to be serializable using a DataContractSerializer
    /// </summary>
    [DataContract]
    public class MyPluginContext : IPluginExecutionContext
    {


        #region IPluginExecutionContext Members
        [IgnoreDataMember]
        public IPluginExecutionContext ParentContext
        {
            get;
            set;
        }

        [DataMember]
        public MyPluginContext Parent
        {
            get;
            set;
        }

        [DataMember]
        public int Stage
        {
            get;
            set;
        }

        #endregion

        #region IExecutionContext Members

        [DataMember]
        public Guid BusinessUnitId
        {
            get;
            set;
        }

        [DataMember]
        public Guid CorrelationId
        {
            get;
            set;
        }

        [DataMember]
        public int Depth
        {
            get;
            set;
        }

        [DataMember]
        public Guid InitiatingUserId
        {
            get;
            set;
        }

        [DataMember]
        public ParameterCollection InputParameters
        {
            get;
            set;
        }

        [DataMember]
        public bool IsExecutingOffline
        {
            get;
            set;
        }

        [DataMember]
        public bool IsInTransaction
        {
            get;
            set;
        }

        [DataMember]
        public bool IsOfflinePlayback
        {
            get;
            set;
        }

        [DataMember]
        public int IsolationMode
        {
            get;
            set;
        }

        [DataMember]
        public string MessageName
        {
            get;
            set;
        }

        [DataMember]
        public int Mode
        {
            get;
            set;
        }

        [DataMember]
        public DateTime OperationCreatedOn
        {
            get;
            set;
        }

        [DataMember]
        public Guid OperationId
        {
            get;
            set;
        }

        [DataMember]
        public Guid OrganizationId
        {
            get;
            set;
        }

        [DataMember]
        public string OrganizationName
        {
            get;
            set;
        }

        [DataMember]
        public ParameterCollection OutputParameters
        {
            get;
            set;
        }


        [DataMember]
        public EntityReference OwningExtension
        {
            get;
            set;
        }

        [DataMember]
        public EntityImageCollection PostEntityImages
        {
            get;
            set;
        }

        [DataMember]
        public EntityImageCollection PreEntityImages
        {
            get;
            set;
        }

        [DataMember]
        public Guid PrimaryEntityId
        {
            get;
            set;
        }

        [DataMember]
        public string PrimaryEntityName
        {
            get;
            set;
        }

        public Guid? RequestId
        {
            get;
            set;
        }

        [DataMember]
        public string SecondaryEntityName
        {
            get;
            set;
        }

        [DataMember]
        public ParameterCollection SharedVariables
        {
            get;
            set;
        }

        [DataMember]
        public Guid UserId
        {
            get;
            set;
        }

        #endregion

        public MyPluginContext()
        {
        }
        
        private MyPluginContext(IPluginExecutionContext context)
        {
            BusinessUnitId = context.BusinessUnitId;
            CorrelationId = context.CorrelationId;
            Depth = context.Depth;
            InitiatingUserId = context.InitiatingUserId;
            InputParameters = context.InputParameters;
            IsExecutingOffline = context.IsExecutingOffline;
            IsInTransaction = context.IsInTransaction;
            IsOfflinePlayback = context.IsOfflinePlayback;
            IsolationMode = context.IsolationMode;
            MessageName = context.MessageName;
            Mode = context.Mode;
            OperationCreatedOn = context.OperationCreatedOn;
            OperationId = context.OperationId;
            OrganizationId = context.OrganizationId;
            OrganizationName = context.OrganizationName;
            OutputParameters = context.OutputParameters;
            OwningExtension = context.OwningExtension;
            PostEntityImages = context.PostEntityImages;
            PreEntityImages = context.PreEntityImages;
            PrimaryEntityId = context.PrimaryEntityId;
            PrimaryEntityName = context.PrimaryEntityName;
            RequestId = context.RequestId;
            SecondaryEntityName = context.SecondaryEntityName;
            SharedVariables = context.SharedVariables;
            Stage = context.Stage;
            UserId = context.UserId;
        }

        public static MyPluginContext Copy(IPluginExecutionContext context)
        {
            var copy = new MyPluginContext(context);
            if (context.ParentContext != null)
            {
                copy.Parent = Copy(context.ParentContext);
            }
            return copy;
        }
    }
}
